﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLControlLibrary.Resources.Controls.Calendar.Common;

namespace SLControlLibrary.Resources.Controls.Calendar.Buttons
{
    public class ucCalendarButton : Button
    {
        #region Dependency Variables
        public static DependencyProperty ButtonTypeProperty = DependencyProperty.Register("ButtonType", typeof(Enumerations.CalendarButtonType), typeof(ucCalendarButton), new PropertyMetadata(Enumerations.CalendarButtonType.Left, new PropertyChangedCallback(ButtonType_Changed)));
        public static DependencyProperty ButtonFillProperty = DependencyProperty.Register("ButtonFill", typeof(SolidColorBrush), typeof(ucCalendarButton), new PropertyMetadata(new SolidColorBrush(Colors.Black)));
        public static DependencyProperty ButtonContentProperty = DependencyProperty.Register("ButtonContent", typeof(DependencyObject), typeof(ucCalendarButton), new PropertyMetadata(null));
        public static DependencyProperty ButtonContentHeightProperty = DependencyProperty.Register("ButtonContentHeight", typeof(double), typeof(ucCalendarButton), new PropertyMetadata(10D));
        public static DependencyProperty ButtonContentWidthProperty = DependencyProperty.Register("ButtonContentWidth", typeof(double), typeof(ucCalendarButton), new PropertyMetadata(10D));
        #endregion

        #region Dependency Properties
        public Enumerations.CalendarButtonType ButtonType
        {
            get { return (Enumerations.CalendarButtonType)this.GetValue(ButtonTypeProperty); }
            set { this.SetValue(ButtonTypeProperty, value); }
        }
        public SolidColorBrush ButtonFill
        {
            get { return (SolidColorBrush)this.GetValue(ButtonFillProperty); }
            set { this.SetValue(ButtonFillProperty, value); }
        }
        public DependencyObject ButtonContent
        {
            get { return (DependencyObject)this.GetValue(ButtonContentProperty); }
            set { this.SetValue(ButtonContentProperty, value); }
        }
        public double ButtonContentHeight
        {
            get { return (double)this.GetValue(ButtonContentHeightProperty); }
            set { this.SetValue(ButtonContentHeightProperty, value); }
        }
        public double ButtonContentWidth
        {
            get { return (double)this.GetValue(ButtonContentWidthProperty); }
            set { this.SetValue(ButtonContentWidthProperty, value); }
        }
        #endregion

        #region Constructor
        public ucCalendarButton()
        {
            DefaultStyleKey = typeof(ucCalendarButton);

            this.Loaded += new RoutedEventHandler(ucHeaderButton_Loaded);
        }
        #endregion

        #region Events
        #region ucHeaderButton_Loaded
        private void ucHeaderButton_Loaded(object sender, RoutedEventArgs e)
        {
            ButtonContent = GetDefaultContent();
        }
        #endregion

        #region ButtonType_Changed
        private static void ButtonType_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ucCalendarButton btn = (ucCalendarButton)sender;
            btn.ButtonType = (Enumerations.CalendarButtonType)e.NewValue;
            btn.ButtonContent = btn.GetDefaultContent();
        }
        #endregion
        #endregion

        #region Methods
        #region GetDefaultContent
        private DependencyObject GetDefaultContent()
        {
            Path _Path = new Path();
            _Path.Width = 10;
            _Path.Opacity = 1;
            _Path.Stretch = Stretch.Uniform;
            Point _Point = new Point(0.5, 0.5);
            _Path.RenderTransformOrigin = _Point;

            if (ButtonType == Enumerations.CalendarButtonType.Left)
            {
                PathGeometry _Geo = new PathGeometry();

                PathFigure _Figure = new PathFigure();
                _Figure.StartPoint = new Point(0, ButtonContentWidth / 2);
                _Figure.IsClosed = true;

                LineSegment _Seg2 = new LineSegment();
                _Seg2.Point = new Point(ButtonContentWidth, 0);

                LineSegment _Seg3 = new LineSegment();
                _Seg3.Point = new Point(ButtonContentHeight, ButtonContentWidth);

                _Figure.Segments.Add(_Seg2);
                _Figure.Segments.Add(_Seg3);

                _Geo.Figures.Add(_Figure);

                _Path.Data = _Geo;
            }
            else if (ButtonType == Enumerations.CalendarButtonType.Right)
            {
                PathGeometry _Geo = new PathGeometry();

                PathFigure _Figure = new PathFigure();
                _Figure.StartPoint = new Point(ButtonContentHeight, ButtonContentWidth / 2);
                _Figure.IsClosed = true;

                LineSegment _Seg2 = new LineSegment();
                _Seg2.Point = new Point(0, ButtonContentWidth);

                LineSegment _Seg3 = new LineSegment();
                _Seg3.Point = new Point(0, 0);
                

                _Figure.Segments.Add(_Seg2);
                _Figure.Segments.Add(_Seg3);

                _Geo.Figures.Add(_Figure);

                _Path.Data = _Geo;
            }
            else if (ButtonType == Enumerations.CalendarButtonType.Down)
            {
                PathGeometry _Geo = new PathGeometry();

                PathFigure _Figure = new PathFigure();
                _Figure.StartPoint = new Point(0, 0);
                _Figure.IsClosed = true;

                LineSegment _Seg2 = new LineSegment();
                _Seg2.Point = new Point(ButtonContentHeight, 0);

                LineSegment _Seg3 = new LineSegment();
                _Seg3.Point = new Point(ButtonContentHeight / 2, ButtonContentWidth / 2);


                _Figure.Segments.Add(_Seg2);
                _Figure.Segments.Add(_Seg3);

                _Geo.Figures.Add(_Figure);

                _Path.Data = _Geo;
            }
            else if (ButtonType == Enumerations.CalendarButtonType.Up)
            {
                PathGeometry _Geo = new PathGeometry();

                PathFigure _Figure = new PathFigure();
                _Figure.StartPoint = new Point(0, ButtonContentWidth / 2);
                _Figure.IsClosed = true;

                LineSegment _Seg2 = new LineSegment();
                _Seg2.Point = new Point(ButtonContentHeight, ButtonContentWidth / 2);

                LineSegment _Seg3 = new LineSegment();
                _Seg3.Point = new Point(ButtonContentHeight / 2, 0);


                _Figure.Segments.Add(_Seg2);
                _Figure.Segments.Add(_Seg3);

                _Geo.Figures.Add(_Figure);

                _Path.Data = _Geo;
            }

            _Path.Fill = ButtonFill;

            return _Path;
        }
        #endregion
        #endregion
    }
}
